package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
	"log"
	"mxshop_srvs/order_srv/model"
	"os"
	"time"
)

type Conn struct {
	DB *gorm.DB
}

func NewConn() (*Conn, error) {
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
		logger.Config{
			SlowThreshold: time.Second,
			LogLevel:      logger.Info,
			Colorful:      true,
		},
	)
	dsn := "root:123456@tcp(127.0.0.1:3306)/mxshop_order_srv?charset=utf8mb4&parseTime=True&loc=Local"

	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true, //取消表名复数s
			//TablePrefix:   "mxshop_",
		},
		Logger: newLogger,
	})
	return &Conn{
		DB: db,
	}, err
}

func main() {
	conn, err := NewConn()
	if err != nil {
		log.Println("conn is error : ", err)
	}
	db := conn.DB

	_ = db.AutoMigrate(&model.ShoppingCart{}, &model.OrderInfo{}, &model.OrderGoods{})

	fmt.Println("sql 执行完成")
}
